package fr.efrei.tradesys.struts.actions.currencies;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import fr.efrei.tradesys.hibernate.beans.AppUser;
import fr.efrei.tradesys.hibernate.beans.Currency;
import fr.efrei.tradesys.hibernate.beans.Owned;
import fr.efrei.tradesys.hibernate.factory.DaoFactory;

public class TradeAction extends ActionSupport
{

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	private static Log				LOGGER						= LogFactory
																									.getLog(TradeAction.class);
	private String						code1;
	private String						code2;
	private String						tradeType;
	private double						bid;
	private double						ask;
	private double						units;

	public String execute() throws Exception
	{
		LOGGER.info("Execution of " + this.getClass().getName());
		Map<String, Object> session = ActionContext.getContext().getSession();
		AppUser u = (AppUser) session.get("user");
		Currency c1 = DaoFactory.getInstance().getCurrencyDao().getCurrency(code1);
		Currency c2 = DaoFactory.getInstance().getCurrencyDao().getCurrency(code2);
		Owned o1 = DaoFactory.getInstance().getOwnedDao().getOwned(u, c1);
		Owned o2 = DaoFactory.getInstance().getOwnedDao().getOwned(u, c2);
		double spent = 0;
		double earned = 0;
		if (tradeType != null
				&& (tradeType.equals("sellBid") || tradeType.equals("sellAsk") || (tradeType
						.equals("buyAsk") || tradeType.equals("buyBid"))))
		{
			if (tradeType.equals("sellBid"))
			{
				spent = units;
				earned = units * bid;
			}
			if (tradeType.equals("sellAsk"))
			{
				spent = units;
				earned = units / ask;
			}
			if (tradeType.equals("buyAsk"))
			{
				spent = units * ask;
				earned = units;
			}
			if (tradeType.equals("buyBid"))
			{
				spent = units / bid;
				earned = units;
			}
			if (o1 != null && o1.getUnits() >= spent)
			{
				o1.setUnits(o1.getUnits() - spent);
				if (o1.getUnits() == 0)
				{
					DaoFactory.getInstance().getOwnedDao().removeOwned(o1);
				}
				else
				{
					DaoFactory.getInstance().getOwnedDao().updateOwned(o1);
				}
				if (o2 != null)
				{
					o2.setUnits(o2.getUnits() + earned);
					DaoFactory.getInstance().getOwnedDao().updateOwned(o2);
				}
				else
				{
					o2 = new Owned(u, c2, earned);
					DaoFactory.getInstance().getOwnedDao().addOwned(o2);
				}
			}
		}
		return SUCCESS;
	}

	public String getCode1()
	{
		return code1;
	}

	public void setCode1(String code1)
	{
		this.code1 = code1;
	}

	public String getCode2()
	{
		return code2;
	}

	public void setCode2(String code2)
	{
		this.code2 = code2;
	}

	public String getTradeType()
	{
		return tradeType;
	}

	public void setTradeType(String tradeType)
	{
		this.tradeType = tradeType;
	}

	public double getBid()
	{
		return bid;
	}

	public void setBid(double bid)
	{
		this.bid = bid;
	}

	public double getAsk()
	{
		return ask;
	}

	public void setAsk(double ask)
	{
		this.ask = ask;
	}

	public double getUnits()
	{
		return units;
	}

	public void setUnits(double units)
	{
		this.units = units;
	}

}
